package com.example.shouybl.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.shouybl.R;
import com.example.shouybl.adapter.GiftAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class GiftFragment extends Fragment implements ViewPager.OnPageChangeListener,RadioGroup.OnCheckedChangeListener{

    private List<Fragment> fragments;
    private ViewPager vpGift;
    private GiftAdapter adapter;
    private RadioGroup rgGift;
    //title布局相关
    private ImageView ivLeft;
    private TextView tvTitle;
    private Button btnRight;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_gift, container, false);
        initView(v);
        adapter = new GiftAdapter(getChildFragmentManager(), fragments);
        vpGift.setAdapter(adapter);
        return v;
    }


    private void initView(View v) {
        //title变量
        ivLeft = (ImageView) v.findViewById(R.id.iv_left);
        tvTitle = (TextView) v.findViewById(R.id.tv_title);
        btnRight = (Button) v.findViewById(R.id.btn_right);
        btnRight.setVisibility(View.GONE);
        //主体变量
        vpGift = (ViewPager) v.findViewById(R.id.vp_gift);
        rgGift = (RadioGroup) v.findViewById(R.id.gift_rg);
        vpGift.setOnPageChangeListener(this);
        rgGift.setOnCheckedChangeListener(this);
        RadioButton radioButton= (RadioButton) rgGift.getChildAt(0);
        radioButton.setChecked(true);

        fragments = new ArrayList<Fragment>();
        fragments.add(GiftMobilFragment.newInstance("1"));
        fragments.add(GiftMobilFragment.newInstance("2"));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }
    @Override
    public void onPageSelected(int position) {
        RadioButton button= (RadioButton) rgGift.getChildAt(position);
        button.setChecked(true);
    }
    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.gift_mobile_btn:
                vpGift.setCurrentItem(0);
                break;
            case R.id.gift_web_btn:
                vpGift.setCurrentItem(1);
                break;
        }
    }
}
